package com.ramblingcoder.bc2.server.tool.update;

import java.io.IOException;
import java.net.Socket;
import java.util.Arrays;
import java.util.Comparator;

import com.ramblingcoder.bc2.server.tool.update.commands.Parcel;
import com.ramblingcoder.bc2.server.tool.update.commands.SupportingFunctions;
import com.ramblingcoder.bc2.server.tool.update.exceptions.InvalidArguments;
import com.ramblingcoder.bc2.server.tool.update.exceptions.InvalidForceKill;
import com.ramblingcoder.bc2.server.tool.update.exceptions.InvalidPlayerName;
import com.ramblingcoder.bc2.server.tool.update.exceptions.InvalidSquadId;
import com.ramblingcoder.bc2.server.tool.update.exceptions.InvalidTeamId;
import com.ramblingcoder.bc2.server.tool.update.exceptions.LogInRequired;
import com.ramblingcoder.bc2.server.tool.update.exceptions.PlayerNotDead;
import com.ramblingcoder.bc2.server.tool.update.exceptions.SetSquadFailed;
import com.ramblingcoder.bc2.server.tool.update.exceptions.SetTeamFailed;
import com.ramblingcoder.bc2.server.tool.update.exceptions.SoldierNotAlive;

public class Player {
	//Variables-----\
	//squad: 0 no squad
	//squad: 1-8 A-H
	//team: 0 neutral
	//team: 1 us
	//team: 2 russian

	public static final String[] TEAMS = {"", "U.S. Army", "Russian Army"};
	public static final String[] SQUADS = {"No Squad", "Alpha", "Bravo", "Charlie", "Delta", "Echo", "Foxtrot", "Golf", "Hotel"};
	
	private String tag;
	private String name;
	private String guid;
	private int squad;
	private int team;
	private int kills;
	private int deaths;
	private int score;
	private int ping;
	//Variables-----/
	
	//Processes-----\	
	public static Player[] listPlayers(Socket socket) throws IOException, InvalidArguments, LogInRequired{
		SupportingFunctions.sendCommand(socket.getOutputStream(), new String[]{"admin.listPlayers", "all"});
		Parcel response = SupportingFunctions.getResponse(socket.getInputStream())[0];
		
		if (response.getStatus().equals("OK")){
			Player[] players = Player.parsePlayers(response);
			
			Arrays.sort(players, new Comparator<Player>() {
				public int compare(Player a, Player b) {
					return a.getName().compareTo(b.getName());
				}
			});
			
			return players;
		}
		else if (response.getStatus().equals("InvalidArguments"))
			throw new InvalidArguments();
		else if (response.getStatus().equals("LogInRequired"))
			throw new LogInRequired();
		
		return null;
	}
	
	private static Player[] parsePlayers(Parcel responses) {
		String[] response = responses.getWords();
		
		Player[] players = new Player[Integer.parseInt(response[11])];
		
		for (String words : response)
			System.out.print(words + " ");
		System.out.println();
		
		for (int i = 12, count = 0; i < response.length; i+=9, count++){
			String tag = response[i];
			String name = response[i+1];
			String guid = response[i+2];
			String team = response[i+3];
			String squad = response[i+4];
			String kills = response[i+5];
			String deaths = response[i+6];
			String score = response[i+7];
			String ping = response[i+8];
			
			Player player = new Player();
			player.setTag(tag);
			player.setName(name);
			player.setGuid(guid);
			player.setTeam(Integer.parseInt(team));
			player.setSquad(Integer.parseInt(squad));
			player.setKills(Integer.parseInt(kills));
			player.setDeaths(Integer.parseInt(deaths));
			player.setScore(Integer.parseInt(score));
			player.setPing(Integer.parseInt(ping));
			
			players[count] = player;
		}
		
		return players;
	}
	
	public static Player findPlayer(Player[] players, String name){
		for (Player player : players)
			if (player.getName().equalsIgnoreCase(name))
				return player;
		
		return null;
	}
	
	public static boolean move(Socket socket, String name, String team, String squad, boolean forceKill) throws IOException, LogInRequired, InvalidArguments, SetSquadFailed, InvalidTeamId, InvalidSquadId, InvalidForceKill, InvalidPlayerName, PlayerNotDead, SetTeamFailed {
		SupportingFunctions.sendCommand(socket.getOutputStream(), new String[]{"admin.movePlayer", name, team, squad, (forceKill ? "true" : "false")});
		Parcel response = SupportingFunctions.getResponse(socket.getInputStream())[0];
		
		if (response.getStatus().equals("OK"))
			return true;
		else if (response.getStatus().equals("InvalidArguments"))
			throw new InvalidArguments();
		else if (response.getStatus().equals("LogInRequired"))
			throw new LogInRequired();
		else if (response.getStatus().equals("InvalidTeamId"))
			throw new InvalidTeamId();
		else if (response.getStatus().equals("InvalidSquadId"))
			throw new InvalidSquadId();
		else if (response.getStatus().equals("InvalidPlayerName"))
			throw new InvalidPlayerName();
		else if (response.getStatus().equals("InvalidForceKill"))
			throw new InvalidForceKill();
		else if (response.getStatus().equals("PlayerNotDead"))
			throw new PlayerNotDead();
		else if (response.getStatus().equals("SetTeamFailed"))
			throw new SetTeamFailed();
		else if (response.getStatus().equals("SetSquadFailed"))
			throw new SetSquadFailed();
			
		return false;
	}

	public static boolean killPlayer(Socket socket, Player player) throws IOException, InvalidArguments, InvalidPlayerName, SoldierNotAlive, LogInRequired {
		SupportingFunctions.sendCommand(socket.getOutputStream(), new String[]{"admin.killPlayer", player.getName()});
		Parcel response = SupportingFunctions.getResponse(socket.getInputStream())[0];
		
		if (response.getStatus().equals("OK"))
			return true;
		else if (response.getStatus().equals("LogInRequired"))
			throw new LogInRequired();
		else if (response.getStatus().equals("InvalidArguments"))
			throw new InvalidArguments();
		else if (response.getStatus().equals("InvalidPlayerName"))
			throw new InvalidPlayerName();
		else if (response.getStatus().equals("SoldierNotAlive"))
			throw new SoldierNotAlive();
		
		return false;
	}
	//Processes-----/

	//Getters/Setters-----\
	public String getGuid() {
		return guid;
	}
	public String getName() {
		return name;
	}
	public int getSquad() {
		return squad;
	}
	public String getTag() {
		return tag;
	}
	public int getTeam() {
		return team;
	}
	public int getDeaths() {
		return deaths;
	}
	public int getKills() {
		return kills;
	}
	public int getPing() {
		return ping;
	}
	public int getScore() {
		return score;
	}
	
	public void setDeaths(int deaths) {
		this.deaths = deaths;
	}
	public void setKills(int kills) {
		this.kills = kills;
	}
	public void setPing(int ping) {
		this.ping = ping;
	}
	public void setScore(int score) {
		this.score = score;
	}
	public void setGuid(String guid) {
		this.guid = guid;
	}
	public void setName(String name) {
		this.name = name;
	}
	public void setSquad(int squad) {
		this.squad = squad;
	}
	public void setTag(String tag) {
		this.tag = tag;
	}
	public void setTeam(int team) {
		this.team = team;
	}
	//Getters/Setters-----/
}
